import { createRouter, createWebHashHistory } from 'vue-router'
import routerMap from './routerMap'
import { getToken } from '@/utils/auth'

const Router = createRouter({
  history: createWebHashHistory(),
  routes: routerMap,
})


const whiteList = ['/login','/home'] // no redirect whitelist
// 全局前置守卫
Router.beforeEach((to, from, next) => {
  //白名单放行
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (!getToken()) {
      // 未登录则重定向到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    } else {
      // 已登录则继续
      next();
    }
  }
});

export default Router
